//
//  BSAFManager.m
//
//
//  Created by lc on 2017/3/15.
//  Copyright © 2017年 lc. All rights reserved.
//

#import "ZJBaseAFManager.h"
#import "ZJUserDefaultsTools.h"
@implementation ZJBaseAFManager

-(instancetype)initWithBaseURL:(NSURL *)url{
    self = [super initWithBaseURL:url];
    if (self) {
        // 请求超时设定
        //
        self.requestSerializer.timeoutInterval = 10;
        self.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [self.requestSerializer setValue:url.absoluteString forHTTPHeaderField:@"Referer"];
        self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];
		
        self.securityPolicy.allowInvalidCertificates = YES;
    }
    return self;

}

+ (instancetype)sharedManager {
    static ZJBaseAFManager *manager = nil;
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{
        
        manager = [[self alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
    });
    return manager;
}

#pragma mark -- 弹出提示框
-(void)showMessage:(NSString *)info{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:info delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}


-(void)requestHttpWithMethod:(HTTPMethod)method withPath:(NSString *)path withDictionary:(NSDictionary *)dictionary withSuccessBlock:(requestSuccessBlock)success withFinishBlock:(requestFinishBlock) finish
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString*lang=[defaults objectForKey:LANGUAGE_SET];//LANGUAGE_SET/Language
    [dictionary setValue:[ZJUserDefaultsTools getCurrentUserToken] forKey:@"TOKEN"];
    //

    if ([lang isEqualToString:@"bo-CN"]) {
        [dictionary setValue:@"bocn" forKey:@"lang"];
		[self.requestSerializer setValue:@"bocn" forHTTPHeaderField:@"lang"];

    }else{
        [dictionary setValue:@"zhcn" forKey:@"lang"];
		[self.requestSerializer setValue:@"zhcn" forHTTPHeaderField:@"lang"];

    }
    switch (method) {
        case GET:{
            [self GET:path parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSLog(@"%@",responseObject);
                if ([responseObject[@"status"] isEqualToNumber:@0]) {
                    success(responseObject);
                }else if([responseObject[@"status"] isEqualToNumber:@134]){
                    NSString* msg= responseObject[@"msg"];
                    [self showMessage:msg];
                   NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
                   [notiCenter postNotificationName:@"tokenLose" object:nil userInfo:nil];//失效发送通知
                   [ZJUserDefaultsTools clearWithoutAvatar];
                //token失效处理
                }else if([responseObject[@"status"] isEqualToNumber:@210]){
                   
                    //token失效处理
                }else if ([responseObject[@"status"] isEqualToNumber:@181]) {
                    success(responseObject);
				}else if ([responseObject[@"status"] isEqualToNumber:@2]){
					success(responseObject);
					
				}else if ([responseObject[@"status"] isEqualToNumber:@3]){
					success(responseObject);

				}
				
				else{
                    NSString* msg= responseObject[@"msg"];
                    [self showMessage:msg];
                }
                finish();
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"请求失败—————%@",error);
               finish();
            }];
            
        }
            break;
            
        case POST:{
            
            [self POST:path parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
               NSLog(@"%@",responseObject);
                
                if ([responseObject[@"status"] isEqualToNumber:@0]) {
                    success(responseObject);
                }else if([responseObject[@"status"] isEqualToNumber:@134]){
                    NSString* msg= responseObject[@"msg"];
                    [self showMessage:msg];
                    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
                    [notiCenter postNotificationName:@"tokenLose" object:nil userInfo:nil];//失效发送通知
                      [ZJUserDefaultsTools clearWithoutAvatar];
                    //token失效处理
                }else {
                    NSString* msg= responseObject[@"msg"];
                     [self showMessage:msg];
 
                }
                //
                finish();
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
   
                 NSLog(@"请求失败————————%@",error);
                //
                finish();
            }];
        }
            break;
            
        case PUT:{
            
            [self PUT:path parameters:dictionary success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSLog(@"%@",responseObject);
          if ([responseObject[@"status"] isEqualToNumber:@0]) {
                    
                    success(responseObject);
                    
                    
          }else if([responseObject[@"status"] isEqualToNumber:@134]){
                   NSString* msg= responseObject[@"msg"];
                    [self showMessage:msg];
              NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
              [notiCenter postNotificationName:@"tokenLose" object:nil userInfo:nil];//失效发送通知
              [ZJUserDefaultsTools clearWithoutAvatar];
              //token失效处理
          }else {
                    NSString* msg= responseObject[@"msg"];
                    [self showMessage:msg];     
                }
                finish();

            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                //统一做处理,关闭loadingview
                NSLog(@"请求失败—————%@",error);
                finish();
            }];
            
        }
            break;
            
            
        case DELETE:{
          [self DELETE:path parameters:dictionary success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              
              NSLog(@"%@",responseObject);
              if ([responseObject[@"status"] isEqualToNumber:@0]) {
                  success(responseObject);
              }else if([responseObject[@"status"] isEqualToNumber:@134]){
                  NSString* msg= responseObject[@"msg"];
                  [self showMessage:msg];
                  NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
                  [notiCenter postNotificationName:@"tokenLose" object:nil userInfo:nil];//失效发送通知
                  [ZJUserDefaultsTools clearWithoutAvatar];
                  //token失效处理
              }else {
                  NSString* msg= responseObject[@"msg"];
                  [self showMessage:msg];
              }
              finish();
          } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              NSLog(@"请求失败—————%@",error);
              finish();
          }];
        
            break;
        }

            
        default:
            break;
    }

}


    


@end
